package com.osdp.sc.controller;

import com.osdp.common.exception.PlatformException;
import com.osdp.common.http.Result;
import com.osdp.common.util.JWTUtil;
import com.osdp.common.util.TokenHolder;
import com.osdp.sc.dto.PermissionDTO;
import com.osdp.sc.entity.ScPermission;
import com.osdp.sc.service.IScPermissionService;
import com.osdp.sc.vo.ScPermissionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 资源管理
 * 如果servvice层有抛出异常，在controller层要try异常，这样才能在界面层显示异常
 *
 * http://cn.voidcc.com/question/p-faamizeg-bhg.html
 *
 * @Date 2022-02-10
 */
@Slf4j
@RestController
@RequestMapping("/sc/permission")
@Api("菜单权限管理")
public class ScPermissionController {

    @Autowired
    private IScPermissionService scPermissionService;

    /**
     * 查询所有资源，以树结构数据返回
     * @return
     */
    @RequestMapping(value = "/getTree", method = RequestMethod.GET)
    @ApiOperation(value = "全量加载菜单树", tags = "全量加载菜单树")
    public Set<ScPermissionVo> queryPermissionTree(){
        List<ScPermission> permissions = scPermissionService.queryAllPermissionList();
        Set<ScPermissionVo> permTreeSet = scPermissionService.createPermissionTree(permissions);
        return permTreeSet;
    }

    /**
     * 异步查询，点击加载数据
     * @param parentId 父节点ID
     * @param parentName 父节点名称，方便界面显示，不需要再次从数据查询，直接从界面转入
     * @return List<ScPermissionVo>
     */
    @RequestMapping(value = "/queryListByParentId", method = RequestMethod.GET)
    @ApiOperation(value = "异步加载权限树", tags = "异步加载权限树")
    public List<ScPermissionVo> queryListByParentId(
            @RequestParam(name = "parentId", required = false) String parentId,
            @RequestParam(name = "parentName", required = false) String parentName){
        return scPermissionService.queryListByParentId(parentId, parentName);

    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation("新增资源")
    public ScPermissionVo insert(@RequestBody ScPermissionVo permissionVo) {
        return scPermissionService.insertScPermission(permissionVo);
    }

    @PutMapping("/update")
    @ApiOperation("修改资源")
    public Result<ScPermissionVo> update(@RequestBody ScPermissionVo permissionVo) {
        ScPermissionVo vo = null;
        try {
            vo = scPermissionService.updateScPermission(permissionVo);
        }catch (PlatformException e){
            return Result.fail(e.getMessage());
        }
        return Result.success(vo);
    }

    /**
     * 删除资源
     *
     * @param id String
     * @return Result
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除资源")
    public Result delete(@RequestParam(name = "id") String id) {
        try {
            scPermissionService.deleteScPermission(id);
        }catch (PlatformException e){
            return Result.fail(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 用于系统登录
     * 查询用户拥有的菜单权限和按钮权限（根据TOKEN）
     *
     * @return List<PermissionVo>
     */
    @RequestMapping(value = "/getUserPermissionByToken", method = RequestMethod.GET)
    public List<PermissionDTO> queryPermissionListByToken(@RequestParam(name="token", required = true) String token){
        String userId = JWTUtil.getUserId(TokenHolder.getToken());
        return scPermissionService.queryPermissionsByUserId(userId);
    }

}
